//index.js
//获取应用实例
const app = getApp();
Page({

      /**
       * 页面的初始数据
       */
      data: {
            index: 1,
            num: 1,
            page: 0, //获取分页第几页
            text: "附近",
            inputText: "",
            orderList: [],
            time: [],
            orderId: null,
            latitude: null,
            longitude: null,
            state: '',
            site: 1,
            distance: null,
            cateId: -1,
            orderListTwo: [],
            status: 5,
            orderCount: null,
            orderText: '',
            number: 0,
            count: 0,
            start: null,
            rate: '',
            complaintCount: 0,
            title: 'personal',
            picSite: 1,
            selectPic: 0,
            loadingHidden: true,
            skill: "",
            mark: '',
            person: "",
            book: "",
            rate: null,

            list: [{
                        iconPath: "../../icons/l1-07.png",
                        selectedIconPath: "../../icons/l1-07-pre.png",
                        selectedColor: "#4EDF80",
                        active: true


                  },
                  {
                        iconPath: "../../icons/l1-04.png",
                        selectedIconPath: "../../icons/l1-04-pre.png",
                        selectedColor: "#4EDF80",
                        active: false
                  },
                  {
                        iconPath: "../../icons/l1-06.png",
                        selectedIconPath: "../../icons/l1-06-pre.png",
                        selectedColor: "#4EDF80",
                        active: false
                  }
            ]
      },

      /**
       * 抢单列表请求
       */
      orderList: function() {
            let that = this;
            if (this.data.orderList.length == 0) {
                  wx.showToast({
                        title: '加载中',
                        icon: 'loading',
                        mask: true
                  })
            }
            wx.request({
                  url: 'https://www.xiaozheng8.com/serviceOrder/findAllGrab/v2',
                  method: "post",
                  data: {
                        // cateId: that.data.cateId,
                        userId: wx.getStorageSync("userId"),
                        page: 0,
                        size: 10,
                  },
                  success: function(res) {
                        console.log(res)
                        console.log('--------------第一次第一次-----------------')
                        if (res.data.code == 200) {
                              that.setData({
                                    orderList: res.data.data,
                              })

                        }
                  },
            });

      },




      go: function(e) {
            var that = this;
            var index = e.currentTarget.dataset.index;
            // this.approve();
            this.setData({
                  site: index + 1,
                  selectPic: index,
                  page: 0
            });
            if (that.data.site == 1) {
                  this.orderList();
                  // this.approve();
            }
            if (that.data.site == 2) {
                  this.orderListTwo();
            }


      },

      /**
       * 订单列表请求
       */
      orderListTwo: function(options) {
            let that = this;
            if (this.data.orderListTwo.length == 0) {
                  wx.showToast({
                        title: '加载中',
                        icon: 'loading',
                        mask: true
                  })
            }
            console.log(wx.getStorageSync("userId"))
            wx.request({
                  url: 'https://www.xiaozheng8.com/serviceOrder/receiveFindAll/v2',
                  method: 'post',
                  data: {
                        userId: wx.getStorageSync("userId"),
                        page: 0,
                        size: 10,
                  },
                  success: function(res) {
                        if (res.data.code === 200) {
                              console.log(res.data.data)
                              console.log("====")
                              that.setData({
                                    orderListTwo: res.data.data,
                                    orderCount: res.data.data.length,
                              })
                        }
                  }
            })
      },

  

      // 认证
      approve: function() {
            let that = this;
            wx.request({
                  url: 'https://www.xiaozheng8.com/receiveUser/findById',
                  method: "post",
                  data: {
                        userId: wx.getStorageSync("userId"),
                  },
                  success: function(res) {
                        console.log(res);
                        console.log(that.data.skill);
                        console.log(that.data.person);
                        console.log(that.data.book);
                        that.setData({
                              mark: res.data.data.mark,
                              rate: res.data.data.rate,
                              starts: res.data.data.starts,
                              complaintCount: res.data.data.complaintCount

                        })
                        console.log(res.data.data.authStatus);
                        var person = res.data.data.authStatus.substr(0, 1);
                        var skill = res.data.data.authStatus.substr(1, 1);
                        var book = res.data.data.authStatus.substr(2, 1);
                        that.setData({
                              skill: skill,
                              person: person,
                              book: book,
                        })
                        // for(var i=0;i<res.data.data.list000.length;i++){

                        // }
                        // for(var i=0;i<res.data.data)
                        // if (res.data.data.isAuth == true) {
                        //   that.setData({
                        //     skill: 1

                        //   })


                        // }
                        // if (res.data.data.authUserId == "" || res.data.data.authUserId == null) {

                        // } else {
                        //   that.setData({
                        //     person: 1
                        //   })
                        // }
                        // if (res.data.data.authSkillId == "" || res.data.data.authSkillId == null) {

                        // } else {
                        //   that.setData({
                        //     book: 1
                        //   })
                        // }
                        console.log(res);
                        console.log(that.data.skill);
                        console.log(that.data.person);
                        console.log(that.data.book);
                  },
            })
      },

 

      /**
       * 生命周期函数--监听页面加载
       */
      onLoad: function(options) {

      },

      /**
       * 生命周期函数--监听页面初次渲染完成
       */
      onReady: function() {

      },



      /**
       * 生命周期函数--监听页面显示
       */
      onShow: function() {
            app.editTabBar();
            this.orderList();
            this.orderListTwo();
            this.approve();
      },

      /**
         * 筛选事件
         */
      screen: function () {
            wx.navigateTo({
                  url: '../screen/screen',
            })
            this.setData({
                  isRuleTrue: false,
                  index: 0,
            })
      },

      /**
       * 综合事件
       */
      comprehensive: function () {
            this.setData({
                  isRuleTrue: false,
                  index: 0,
            })
      },

      translate: function (e) {
            // console.log(e);
            this.setData({
                  isRuleTrue: true,
                  index: 0,
            })

      },

      success: function () {
            this.setData({
                  isRuleTrue: false,
                  index: 1
            })
      },

      changeBgc: function (e) {
            this.setData({
                  num: e.target.dataset.num,
                  //   isRuleTrue: false,
                  index: 0,
            })
      },

      sreach: function () {
            this.setData({
                  isTrue: true,
            })
      },

      text: function (e) {
            let that = this;
            // console.log(e);
            that.setData({
                  text: e.target.dataset.text
            })
      },

      /**
        * 切换身份事件
        */
      change: function () {
            wx.navigateTo({
                  url: '../changeId/changeId',
            })
            wx.setStorageSync("type", null);
      },

      /**
       * 设置事件
       */
      toSetting: function () {
            wx.navigateTo({
                  url: '../receivingSetting/receivingSetting',
            })
      },

      /**
       * 进入身份认证事件函数
       */
      technology: function () {
            wx.navigateTo({
                  url: '../technology/technology',
            })
      },

      /**
       * 进入服务内容事件函数
       */
      server: function () {
            wx.navigateTo({
                  url: '../receivingServer/receivingServer',
            })
      },

      /**
        * 进入搜索
        */
      search: function () {
            wx.navigateTo({
                  url: '../receivingSearch/receivingSearch',
            })
      },

      /**
       * 抢单事件函数
       */
      toMyOrder: function (e) {
            var that = this;
            // this.approve();
            console.log("---houmian--")
            console.log(that.data.skill)
            var info = "";
            if (that.data.skill == null || that.data.skill == "" || that.data.skill == undefined || that.data.skill == 0 || that.data.skill == 1) {
                  info = " '服务内容' ";
            }
            if (that.data.person == null || that.data.person == "" || that.data.person == undefined || that.data.person == 0 || that.data.person == 1) {
                  info = info + " '身份认证' ";
            }
            if (that.data.book == null || that.data.book == "" || that.data.book == undefined || that.data.book == 0 || that.data.book == 1) {
                  info = info + " '技能证书' ";
            }
            var that = this;

            if (info.length > 0) {
                  wx.showModal({
                        title: "提示",
                        content: '请您进入个人中心完善' + info + "后再来抢单哦",
                        showCancel: false,
                        success: function (res) {
                              if (res.confirm) {
                                    that.setData({
                                          site: 3,
                                    })
                              }
                        }
                  })
                  return;
            } else {

                  // console.log(e)
                  that.setData({
                        publishId: e.currentTarget.dataset.publishid,
                        distance: e.currentTarget.dataset.distance
                  })
                  // console.log(e.currentTarget.dataset.orderid)

                  wx.navigateTo({
                        url: '../grabOrder/grabOrder?publishId=' + that.data.publishId + '&distance=' + that.data.distance,
                  })
                  console.log(that.data.publishId)
            }

      },

      /**
       * 筛选未完成订单
       */
      unFinish: function () {
            wx.navigateTo({
                  url: '../unfinish/unfinish',
            })
      },

      /**
       * 空白页面去抢单事件
       */
      goGrabOrder: function () {
            this.setData({
                  site: 1,
                  selectPic: 0,
            })
      },


      /**
       * 请求抢单列表下一页
       */
      nextOrderPage: function() {
            let that = this;
            wx.request({
                  url: 'https://www.xiaozheng8.com/serviceOrder/findAllGrab/v2',
                  method: "post",
                  data: {
                        cateId: that.data.cateId,
                        userId: wx.getStorageSync("userId"),
                        page: that.data.page+1,
                        size: 10,
                  },
                  success: function(res) {
                        console.log(res.data.data)
                        console.log("page" + ++that.data.page)
                        console.log('-------------------------------')
                        if (res.data.code == 200) {
                              that.setData({
                                    page: that.data.page + 1,
                                    orderList: that.data.orderList.concat(res.data.data),
                              })
                        }
                  },
            });
      },


      /**
       * 进入订单详情
       */
      detail: function (e) {

            var that = this;
            console.log(e + "zzzz")
            that.setData({
                  orderId: e.currentTarget.dataset.orderid,
            })
            console.log(that)
            wx.navigateTo({
                  url: '../grabSuccess/grabSuccess?orderId=' + that.data.orderId,
            })

      },

      /**
       * 已抢订单无法点击弹窗事件
       */
      noClick: function () {
            wx.showModal({
                  title: '提示',
                  content: '该订单已经被抢走了',
                  confirmText: '确定',
                  confirmColor: '#0bbd79',
                  showCancel: false
            })
      },

      /**
       * 请求订单列表下一页
       */
      nextMinePage: function (){
            let that = this;
            wx.request({
                  url: 'https://www.xiaozheng8.com/serviceOrder/receiveFindAll/v2',
                  method: 'post',
                  data: {
                        userId: wx.getStorageSync("userId"),
                        page: that.data.page+1,
                        size: 10,
                  },
                  success: function (res) {
                        console.log(that.data.page)
                        if (res.data.code === 200) {
                              that.setData({
                                    page: that.data.page + 1,
                                    orderListTwo: that.data.orderListTwo.concat(res.data.data),
                              })
                        }
                  }
            })
      },

      /**
       * 进入我的订单
       */
      order: function (e) {
            let a = e.currentTarget.dataset.class;
            wx.navigateTo({
                  url: '../myOrder/myOrder?userId=' + wx.getStorageSync("userId")+'&class='+ a,
            })
      },

      /**
       * 进入身份认证
       */
      user: function () {
            if (this.data.title = "personal") {
                  console.log(this.data.title)
                  wx.navigateTo({
                        url: '../personal/personal'
                  })
            } else {
                  wx.navigateTo({
                        url: '../enterprise/enterprise',
                  })
            }

      },


      /**
       * 生命周期函数--监听页面隐藏
       */
      onHide: function() {

      },

      /**
       * 生命周期函数--监听页面卸载
       */
      onUnload: function() {

      },

      /**
       * 页面相关事件处理函数--监听用户下拉动作
       */
      onPullDownRefresh: function() {

      },

      /**
       * 页面上拉触底事件的处理函数
       */
      onReachBottom: function() {

      },

      /**
       * 用户点击右上角分享
       */
      onShareAppMessage: function() {

      }
})